package com.ptszyxx.popose.module.main.ticket.vm;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.module.main.MainVM;
import com.ysg.http.data.entity.ticket.TicketPriceEntity;
import com.ysg.utils.YChannelUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.mine.PayEntity;
import com.ysg.http.data.entity.ticket.TicketEntity;
import com.ysg.utils.YConfigUtil;
import com.ysg.utils.YPhoneUtil;
import com.ysg.utils.YStringUtil;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observable;

public class TicketDialogVM extends BaseViewModel<CommonRepository> {
    public String payId;
    public ObservableField<String> payPrice = new ObservableField<>();
    public boolean isJumpPay;
    public String orderInfo;   // 订单信息

    public TicketDialogVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onAlipayEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onWechatPayCommand = new BindingCommand(() -> requestPay("2"));
    public BindingCommand onAliPayCommand = new BindingCommand(() -> requestPay("1"));

    /**
     * 支付
     */
    public void requestPay(String payType){
        if (YStringUtil.eq(1, payType) && YConfigUtil.getPayApp()){
            requestPayApp(payType);
            return;
        }


        Map<String, String> map = new HashMap<>();
        map.put("type", "3");
        map.put("projectId", payId);
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("channel", YChannelUtil.getInstance().getChannel());

        Observable observable = model.payAlipay(map);
        if (YStringUtil.eq(2, payType)){
            observable = model.payWechat(map);
        }

        HttpUtils.getInstance()
                .data(observable, this, new OnSuccessResult<PayEntity>() {
                    @Override
                    public void onSuccessResult(BaseResponse<PayEntity> response) {
                        YActivityUtil.getInstance().jumpPay(TicketDialogVM.this, response.getData().getPayUrl(), payType);
                        isJumpPay = true;
                    }
                });
    }

    /**
     * 支付
     */
    public void requestPayApp(String payType){
        Map<String, String> map = new HashMap<>();
        map.put("type", "3");
        map.put("projectId", payId);
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("channel", YChannelUtil.getInstance().getChannel());

        HttpUtils.getInstance()
                .data(model.payAlipayApp(map), this, new OnSuccessResult<String>() {
                    @Override
                    public void onSuccessResult(BaseResponse<String> response) {
                        orderInfo = response.getData();
                        uc.onAlipayEvent.call();
                    }
                });
    }

    /**
     * 门票
     */
    public void requestTicket(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.tickUserInfo(map), this,
                        new OnSuccessResult<TicketEntity>() {
                            @Override
                            public void onSuccessResult(BaseResponse<TicketEntity> response) {
                                if (YStringUtil.eq(1, response.getData().getWindowTicket())){
                                    YActivityUtil.getInstance().close(TicketDialogVM.this);
                                }
                                try {
                                    Log.e("门票是否购买", ""+response.getData().getWindowTicket());
                                }catch (Exception e){
                                }
                            }
                        });
    }

    /**
     * 门票价格
     */
    public void requestTicketPrice(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.tickPrice(map), this,
                        new OnSuccessResult<TicketPriceEntity>() {
                            @Override
                            public void onSuccessResult(BaseResponse<TicketPriceEntity> response) {
                                payId = response.getData().getId();
                                payPrice.set(response.getData().getPrice());
//                                uc.onPriceEvent.call();
                            }
                        });
    }







}
